const app = getApp()
import {
  list,
  count
} from '../../../api/coupon'
import {
  add,
  buyNow
} from '../../../api/cart'
import {
  find
} from '../../../api/product';
import {
  buyNowForCoupon
} from '../../../api/cart';
import {
	memberList,receiverList,cancel,confirm
} from "../../../api/couponCodeGift"
Page({
  data: {
    exchangetCouponList: [],
		currentTab: 0,
		memberListForm:[],
		receiverListForm:[]
  },
  onLoad: function (options) {
    var that = this;
    /**
     * 获取当前设备的宽高
     */
    that.data.currentTab = options.currentTab
    this.setData({
      currentTab: that.data.currentTab
    })
  },
  onShow: function () {
    this.setNavSize()
		this.getCouponData()
  },
  setNavSize: function () {
    var that = this,
      sysinfo = wx.getSystemInfoSync(),
      statusHeight = sysinfo.statusBarHeight,
      isiOS = sysinfo.system.indexOf('iOS') > -1,
      navHeight;
    console.log(sysinfo)
    if (!isiOS) {
      navHeight = 48;
    } else {
      navHeight = 44;
    }
    that.setData({
      status: statusHeight,
      navHeight: navHeight
    })
  },
  reachBottom(){
    var _this = this
    // if (_this.data.loading == true) {
    //   this.data.pageNum = this.data.pageNum + 1;
    //   this.loadList()
    // }
  },
  //获取优惠券的数据
	getCouponData(callback) {
		list({
			pageSize: 1000
		}).then(
			res => {
				if (res.type == 'success') {
					if (res.data.data.length > 0) {
						let exchangetCouponList = []
						let exchangetShopList = []
						res.data.data.map((item) => {
							if (item.type == 3) {
								exchangetShopList.push(item.mchName)
							}
						})
						exchangetShopList = Array.from(new Set(exchangetShopList))
						if (exchangetShopList.length > 0) {
							exchangetShopList.map((item) => {
								let exchangetCouponListItem = {}
								exchangetCouponListItem.couponList = []
								res.data.data.map((item1) => {
									if (item1.mchName == item) {
										if (item1.type == 3) {
											exchangetCouponListItem.chk = false
											exchangetCouponListItem.mchName = item1.mchName
											exchangetCouponListItem.mchLogo = item1.mchLogo
											if (item1.endDate==null) {
												item1.endDate = '永久有效'
											} else {
												item1.endDate = this.formatDateAndTime(item1.endDate)
											}
											item1.tag = this.formatTag(item1.targetType)
											item1.chk = false
											item1.quantitys = 1
											exchangetCouponListItem.couponList.push(item1)
										}
									}
								})
								exchangetCouponList.push(exchangetCouponListItem)
							})
						}
						this.setData({
							exchangetCouponList: exchangetCouponList
						})
					}
				}
			}
		).catch(error => {
			console.log(error)
		})
  },
  //时间格式化器
	formatDateAndTime: function(value) {
		let date = new Date(value);
		let y = date.getFullYear();
		let MM = date.getMonth() + 1;
		MM = MM < 10 ? ('0' + MM) : MM;
		let d = date.getDate();
		d = d < 10 ? ('0' + d) : d;
		let h = date.getHours();
		let m = date.getMinutes();
		let s = date.getSeconds();
		return y + '-' + MM + '-' + d + " " + h + ":" + m + ":" + s;
  },
  //适用范围的初始化
	formatTag(value) {
		let rusult = ""
		switch (value) {
			case 0:
				rusult = "全场通用"
				break;
			case 1:
				rusult = "指定品牌"
				break;
			case 2:
				rusult = "会员专属"
				break;
			case 3:
				rusult = "指定品类"
				break;
			case 4:
				rusult = "指定商品"
				break;
			case 5:
				rusult = "指定商品兑换"
				break;
		}
		return rusult
  },
  useClick(){
		var _this = this
		var exchangetCouponList = []
		_this.data.exchangetCouponList.forEach(function(item){
			item.couponList.forEach(function(items){
				if(items.chk == true){
					var data = {
						id:items.id,
						quantity:items.quantitys
					}
					exchangetCouponList.push(data)
				}
			})
		})
		if(exchangetCouponList.length > 0){
			buyNowForCoupon(exchangetCouponList).then(
				res => {
					if (res.type == 'success') {
						wx.navigateTo({
							url: '/pages/goods/submit/index',
						})
					}
				}
			).catch(error => {
				wx.showToast({
					title: error.content,
					icon: 'none',
					duration: 2000
				})
			})
		}
  },
  numAdd(e){
		console.log(e)
		var _this = this
		var index = e.currentTarget.dataset.index
		var indexs = e.currentTarget.dataset.indexs
		if(this.data.exchangetCouponList[index].couponList[indexs].quantitys >= this.data.exchangetCouponList[index].couponList[indexs].stock){
			this.data.exchangetCouponList[index].couponList[indexs].quantitys = this.data.exchangetCouponList[index].couponList[indexs].stock
			wx.showToast({
				title: '剩余张数不足',
			})
		}else{
			this.data.exchangetCouponList[index].couponList[indexs].quantitys += 1
		}
		_this.setData({
			exchangetCouponList:_this.data.exchangetCouponList
		})
	},
	numReduce(e){
		var _this = this
		var index = e.currentTarget.dataset.index
		var indexs = e.currentTarget.dataset.indexs
		if(this.data.exchangetCouponList[index].couponList[indexs].quantitys <= 1){
			this.data.exchangetCouponList[index].couponList[indexs].quantitys = 1
			wx.showToast({
				title: '张数不能少于1',
			})
		}else{
			this.data.exchangetCouponList[index].couponList[indexs].quantitys -= 1
		}
		_this.setData({
			exchangetCouponList:_this.data.exchangetCouponList
		})
	},
	bindblur(e){
		var _this = this
		console.log(e)
		var index = e.currentTarget.dataset.index
		var indexs = e.currentTarget.dataset.indexs
		if(this.data.exchangetCouponList[index].couponList[indexs].quantitys < 1){
			this.data.exchangetCouponList[index].couponList[indexs].quantitys = 1
			wx.showToast({
				title: '张数不能少于1',
			})
		}
		if(this.data.exchangetCouponList[index].couponList[indexs].quantitys > this.data.exchangetCouponList[index].couponList[indexs].stock){
			this.data.exchangetCouponList[index].couponList[indexs].quantitys = this.data.exchangetCouponList[index].couponList[indexs].stock
			wx.showToast({
				title: '剩余张数不足',
			})
		}
		_this.setData({
			exchangetCouponList:_this.data.exchangetCouponList
		})
  },
  chooseClick(e){
		var index = e.currentTarget.dataset.index
		var indexs = e.currentTarget.dataset.indexs
		console.log(this.data.exchangetCouponList)
		this.data.exchangetCouponList[indexs].couponList[index].chk = !this.data.exchangetCouponList[indexs].couponList[index].chk
		console.log(this.data.exchangetCouponList)
		this.setData({
			exchangetCouponList:this.data.exchangetCouponList
		})
  },
  bindChange: function(e) {
		var that = this;
		console.log(e)
		if(e.detail.current == 0){
			this.getCouponData()
		}else if(e.detail.current == 1){
			this.memberListClick()
		}else{
			this.receiverListClick()
		}
		that.setData({
			currentTab: e.detail.current
		});
  },
  	//  tab切换逻辑
	swichNav: function(e) {
		console.log(e)
		var that = this;
		if (this.data.currentTab === e.target.dataset.current) {
			return false;
		} else {
			that.setData({
				currentTab: e.target.dataset.current
			})
		}
  },
  selectAllClick(e){
		var _this = this
		console.log(e)
		var index = e.currentTarget.dataset.index
		if(_this.data.exchangetCouponList[index].chk == false){
			_this.data.exchangetCouponList[index].chk = true
			_this.data.exchangetCouponList[index].couponList.forEach(function(item){
				item.chk = true
			})
		}else{
			_this.data.exchangetCouponList[index].chk = false
			_this.data.exchangetCouponList[index].couponList.forEach(function(item){
				item.chk = false
			})
		}
		_this.setData({
			exchangetCouponList:_this.data.exchangetCouponList
		})
  },
  giveClick(){
    var _this = this
    var exchangetCouponList = []
		_this.data.exchangetCouponList.forEach(function(item){
			item.couponList.forEach(function(items){
				if(items.chk == true){
					exchangetCouponList.push(items)
				}
			})
    })
    if(exchangetCouponList.length > 0){
      this.selectComponent("#give").giveClick(exchangetCouponList)
    }else{
      wx.showToast({
        title: '请选择兑换券',
      })
    }
	},
	memberListClick(){
		var _this = this
		memberList({
			pageSize: 1000
		}).then(res=>{
			console.log(res)
			console.log('memberList')
			_this.data.memberListForm = res.data.data
			_this.data.memberListForm.forEach(function(item){
				if (item.endDate==null) {
					item.couponCodeVo.endDate = '永久有效'
				} else {
					item.couponCodeVo.endDate = this.formatDateAndTime(item.endDate)
				}
			})
			console.log(_this.data.memberListForm)
			_this.setData({
				memberListForm:_this.data.memberListForm
			})
		})
	},
	receiverListClick(){
		var _this = this
		receiverList({
			pageSize: 1000
		}).then(res=>{
			console.log(res)
			console.log('receiverList')
			_this.data.receiverListForm = res.data.data
			_this.data.receiverListForm.forEach(function(item){
				if (item.endDate==null) {
					item.couponCodeVo.endDate = '永久有效'
				} else {
					item.couponCodeVo.endDate = this.formatDateAndTime(item.endDate)
				}
			})
			_this.setData({
				receiverListForm:_this.data.receiverListForm
			})
		})
	},
	cancelClick(data){
		console.log(data)
		var index = data.currentTarget.dataset.index
		var _this = this
		if(_this.data.memberListForm[index].status == 0){
			var form = {
				couponId:_this.data.memberListForm[index].couponId,
				couponCodeId:_this.data.memberListForm[index].couponCodeId,
				quantity:_this.data.memberListForm[index].quantity,
				receiverId:_this.data.memberListForm[index].memberId,
				id:_this.data.memberListForm[index].id
			}
			cancel(form).then(res=>{
				console.log(res)
				wx.showToast({
					title: '撤销成功',
				})
				_this.memberListClick()
			})
		}
	},
	confirmClick(data){
		console.log(data)
		var index = data.currentTarget.dataset.index
		var _this = this
		if(_this.data.receiverListForm[index].status == 0){
			var form = {
				couponId:_this.data.receiverListForm[index].couponId,
				couponCodeId:_this.data.receiverListForm[index].couponCodeId,
				quantity:_this.data.receiverListForm[index].quantity,
				receiverId:_this.data.receiverListForm[index].memberId,
				id:_this.data.receiverListForm[index].id
			}
			confirm(form).then(res=>{
				console.log(res)
				wx.showToast({
					title: '领取成功',
				})
				_this.receiverListClick()
			})
		}
	},
	couponSave(){
		this.getCouponData()
	}
})
